package com.kanq.handler;

import com.kanq.common.utils.SseUtil;
import com.kanq.pojo.context.ChatContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;


/**
 * 责任链处理器链
 */
@Slf4j
public class ChatHandlerChain implements ChatHandler {
    private final List<ChatHandler> handlers;

    public ChatHandlerChain(ChatHandler... handlers) {
        this.handlers = Arrays.asList(handlers);
    }

    @Override
    public void handle(ChatContext context) {
        try {
            for (ChatHandler handler : handlers) {
                handler.handle(context);
            }
        } catch (Exception e) {
            context.getResponse().setStage("end");
            context.getResponse().setContent("您好，暂时无法回答您的问题，请换个问题试试。");
            SseUtil.sseSend(context.getEmitter(), context.getResponse());
            context.getEmitter().complete();
            log.error("处理聊天请求时发生异常", e);
        }
    }
}